/*####################################################################################
  CLASS : mydojo.dijix.SimpleTree
  ==================================
  - A simple tree widget
  
  - Issues : cannot recreate tree using same this.data, but can create using same data struct but different variable source
  - TODO : insert new item in model and update tree. do not update the entire tree
  
  - NOTES:
  		- dojo.connect(this.tree, "onClick" , function(){ alert("u clicked a node: " + this.tree.lastFocused.item.name)});
     
  By: P.M.Shrestha  
  ####################################################################################*/
dojo.provide("mydojo.dijix.SimpleTree");

dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.Tree");
dojo.require("dijit.layout.ContentPane");

dojo.declare("mydojo.dijix.SimpleTree", dijit.layout.ContentPane , {

		data 		:{	"label":"name",
						"identifier":"name",
						"items":[	{"type":"sub","name":"Transfer",
									 "children":[	{"_reference":"Heading 1"},
													{"_reference":"Heading 2"},
													{"_reference":"Heading 3"} ]},
									{"type":"sub","name":"Leave Message"},
									{"type":"sub","name":"Information"},
									{"type":"sub","name":"Fax On Demand"},
									{"type":"sub","name":"Fax IN"},
									
									{"type":"main","name":"Maiinmenu",
											"children":[	{"_reference":"Transfer"},
															{"_reference":"Leave Message"},
															{"_reference":"Information"},
															{"_reference":"Fax On Demand"},
															{"_reference":"Fax IN"}
														]
									},
									{"type":"sub","name":"Heading 1"},
									{"type":"sub","name":"Heading 2"},
									{"type":"sub","name":"Heading 3"}									
							  	]
						},
							
		/* { 'identifier': 'name',
						  'label': 'name',
						  'items': [
									 {"name":["Transfer"],	 "type":"sub"},
									 {"name":"Fax On Demand","type":"sub"},
									 {"name":"Fax In",		 "type":"sub"},
									 {"name":"MainMenu",	 "type":"continent",
									   "children":[{"_reference":"Transfer"},
									               {"_reference":"Fax On Demand"},
									               {"_reference":"Fax In"}
									              ]}
									]
					/*[
						     
						     { "name":"Egypt", "type":"country" , "id": 1 },
						     { "name":"Kenya", "type":"country", "id": 2},
						     { "name":"Sudan", "type":"country", "id": 3},
						     { "name":"Africa", "type":"continent",
						         "children":[{"_reference":"Egypt"}, {"_reference":"Kenya"}, {"_reference":"Sudan"}] }						    
						]},*/				
						
		tree: null ,
		st : null,
		model : null,
		queryType : "main",//"continent",
		// ======================================================================
		//	Post create widget - Create grid widget inside the CP
		// ----------------------------------------------------------------------
		postCreate: function() {		
			this.inherited(arguments);				
			this.renderTree();			
		},
		// ======================================================================
		//	Render tree
		// ----------------------------------------------------------------------	
		renderTree: function(response){
			if(!response)	response = this.data;			
			if( this.tree ){				
				this.tree.destroyDescendants();
				this.tree.destroy();
			}
			this.st = new dojo.data.ItemFileReadStore( { data: response } );			
			this.model = new dijit.tree.ForestStoreModel( {
				store: this.st,
				query: {type:this.queryType},
				rootId: "",
				rootLabel: "",
				childrenAttrs: ["children"]
			} );
			this.tree = new dijit.Tree({ model: this.model },document.createElement("div"));
			this.domNode.appendChild(this.tree.domNode);
			this.tree.startup();		
			
			// override onClick action
			this.tree.onClick = this.nodeClicked;
		},
		// ======================================================================
		//	Render Test Function
		// ----------------------------------------------------------------------	
		renderAgain: function(){
			var d =  { 'identifier': 'name',
						  'label': 'name',
						  'items': [
						     { 'name':'Africa', 'type':'continent',
						         'children':[{'_reference':'Egypt'}, {'_reference':'Kenya'}, {'_reference':'Sudan'}] },
						     { 'name':'Egypt', 'type':'country' , 'id': 1 },
						     { 'name':'Kenya', 'type':'country', 'id': 2},
						     { 'name':'Sudan', 'type':'country', 'id': 3}
						]};
			this.renderTree(d);
		},
		// ======================================================================
		//	Tree node clicked action - override this to do something meaningful
		// ----------------------------------------------------------------------	
		nodeClicked: function(item,node){
			//console.log("tree ndoe clicked "  +  item.name + " , " + node );
		},
		// ======================================================================
		//	Get last focused item node from tree
		// ----------------------------------------------------------------------	
		getFocusedNode: function(){
			return this.tree.lastFocused.item;
		}		
});


